<?php
namespace app\admin\model;

use think\Model;

class Role extends Model
{

    // protected $autoWriteTimestamp = true;
    // protected $autoWriteTimestamp = 'datetime';
    
    // 定义时间戳字段名
    protected $createTime = '';

    protected $updateTime = '';

    public function saveData($data, $cId, $boolean = FALSE)
    {
        if (empty($data))
            return [
                400,
                'No updated data'
            ];
        
        if (! empty($data['id']))
            $boolean = true;
        
        $value = [];
        $value['name'] = ! empty($data['name']) ? $data['name'] : '';
        $value['rules'] = ! empty($data['rules']) ? implode(',', $data['rules']) : '';
        $value['rules1'] = ! empty($data['rules1']) ? implode(',', $data['rules1']) : '';
        $value['info'] = ! empty($data['info']) ? $data['info'] : '';
        $value['status'] = ! empty($data['status']) ? $data['status'] : 0;
        
        if ($boolean) {
            if (empty($data['id']))
                return [
                    400,
                    '修改地址不合法'
                ];
            
            $result = $this->where('id', $data['id'])
                ->where('u_id', $cId)
                ->update($value);
            if (! $result)
                return [
                    400,
                    '更新失败'
                ];
        } else {
            $value['u_id'] = $cId;
            $result = $this->save($value);
            if (! $result)
                return [
                    400,
                    '增加失败'
                ];
        }
        
        return [
            200,
            'success'
        ];
    }
}
